package net.huashitong.goodsclass.web;

import com.sirdc.modules.core.exceptions.UserExistException;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.util.UserUtils;
import net.huashitong.goodsclass.entity.GoodsClasses;
import net.huashitong.goodsclass.filter.GoodsClassFilter;
import net.huashitong.goodsclass.filter.StatusFilter;
import net.huashitong.goodsclass.service.GoodsClassesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/goods/class")
public class GoodsClassesCtrl extends JsonBaseController {
    @Autowired
    private GoodsClassesService goodsClassesService;

    @Override
    protected String getView(String s) {
        return "/modules/goodsclass/"+s;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid query(@ModelAttribute GoodsClassFilter filter) {
        List<GoodsClasses> goodsClasses = goodsClassesService.queryPageList(filter);
        return coverJqGrid(filter, goodsClasses);
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showAddView(@ModelAttribute GoodsClassFilter filter, Model model) {
        //根据自己的过滤规则过滤部门，将来可以考虑是否要修改成下拉树
        List<StatusFilter> list = new ArrayList<>();
        StatusFilter statusFilter = new StatusFilter();
        statusFilter.setName("正常");
        statusFilter.setValue("1");
        list.add(statusFilter);

        StatusFilter statusFilter2 = new StatusFilter();
        statusFilter2.setName("停用");
        statusFilter2.setValue("0");
        list.add(statusFilter2);

        GoodsClasses goodsClasses = new GoodsClasses();
        model.addAttribute("databean",goodsClasses);
        model.addAttribute("statusList",list);
        return getView("form");
    }

    @RequestMapping("/filter")
    public String filter(@ModelAttribute GoodsClassFilter filter) {
        return getView("filter");
    }



    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute GoodsClassFilter datebean) {
        try{
            GoodsClasses obj = new GoodsClasses();
            BeanUtils.copyProperties(datebean,obj);
            obj.setSysId(null);
            goodsClassesService.save(obj);

        }catch (Exception e){
            throw e;
        }
        return coverMessage("200");
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        //根据自己的过滤规则过滤部门，将来可以考虑是否要修改成下拉树
        List<StatusFilter> list = new ArrayList<>();
        StatusFilter statusFilter = new StatusFilter();
        statusFilter.setName("正常");
        statusFilter.setValue("1");
        list.add(statusFilter);

        StatusFilter statusFilter2 = new StatusFilter();
        statusFilter2.setName("停用");
        statusFilter2.setValue("0");
        list.add(statusFilter2);

        GoodsClasses goodsClasses = goodsClassesService.getById(id);
        model.addAttribute("databean",goodsClasses);
        model.addAttribute("statusList",list);
        return getView("form");
    }

    /**
     * 更新用户表操作
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute GoodsClasses datebean) {
//        sysUserService.updateUserLogin(datebean);
        goodsClassesService.update(datebean);
        return coverMessage("200");
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}")
    public Message delete(@PathVariable List<String> id) {
//        boolean del = id.remove(UserUtils.getUserId());
//        sysUserService.batchDelete(id);
//        if(del) {
//            return coverMessage("500", "删除成功，但您不能删除自己");
//        }
        goodsClassesService.batchDelete(id);
        return coverMessage("200");
    }






}
